//
//  MyHomeController.m
//  SimpleHappy
//
//  Created by lanou on 15/11/29.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "MyHomeController.h"
#import "Define.h"
#import <BmobSDK/Bmob.h>
#import "FindPWController.h"
#import "DIYButton.h"

#import <AssetsLibrary/AssetsLibrary.h>


@interface MyHomeController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImageView *icon; // 个人头像
@property (nonatomic, strong) UILabel *userLabel; // 用户名
@property (nonatomic, strong) UIButton *quiteBtn; // 注销
@property (nonatomic, strong) UILabel *emailLabel; // 显示用户邮箱
@property (nonatomic, strong) BmobUser *user; // 用户的信息

@property (nonatomic, strong) UIButton *findPW;
@end

@implementation MyHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [[super registBtn]removeFromSuperview];
    [super myTitleLabel].text = @"个人中心";
    self.navigationController.navigationBar.translucent = NO;


    // 获取用户信息
    self.user = [BmobUser getCurrentUser];
    
    // 个人头像
    self.icon = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth / 3, KScreenWidth / 4.5, KScreenWidth / 3, KScreenWidth / 3)];
    self.icon.layer.cornerRadius = KScreenWidth / 6;
    self.icon.backgroundColor = [UIColor blackColor];
    self.icon.image = [UIImage imageNamed:@"long"];
    // 头像添加点击事件
//    self.icon.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//    [self.icon addGestureRecognizer:tap];
    [self.view addSubview:_icon];
    
    // 用户名
    self.userLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, KScreenWidth / 1.5, KScreenWidth, KScreenWidth / 12)];
    self.userLabel.text = [NSString stringWithFormat:@"%@,您好!",_user.username];
    self.userLabel.font = [UIFont systemFontOfSize:20];
    self.userLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_userLabel];
    
    // 显示邮箱
//    if (self.user.email != nil) {
        UILabel *youxiang = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth / 4, KScreenHeight /2, KScreenWidth / 1.5, KScreenWidth / 10)];
        youxiang.text = @"邮箱:";
        [self.view addSubview:youxiang];
        
        self.emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth / 2.5, KScreenHeight /2, KScreenWidth / 2, KScreenWidth / 10)];
        NSString *emailStr = [self.user objectForKey:@"email"];
        
        // 邮箱显示格式
        NSString *linkStr = [emailStr componentsSeparatedByString:@"@"].firstObject;
        if (linkStr.length > 4) {
            linkStr = [linkStr stringByReplacingOccurrencesOfString:[linkStr substringFromIndex:4] withString:@"***"];
        }
        NSString *houStr = [emailStr componentsSeparatedByString:@"@"].lastObject;
        self.emailLabel.text = [NSString stringWithFormat:@"%@@%@",linkStr,houStr];
        [self.view addSubview:_emailLabel];
//     }
    // 是否验证
    DIYButton *isVerfied = [DIYButton buttonWithType:UIButtonTypeCustom];
    isVerfied.frame = CGRectMake(KScreenWidth / 4, KScreenHeight / 1.7, KScreenWidth / 1.5, KScreenWidth / 15);
    if ([_user objectForKey:@"emailVerified"]) {
        isVerfied.iconImageView.image = [UIImage imageNamed:@"dui"];
        isVerfied.textLabel.text = @"邮箱已验证";
    }else{
        isVerfied.iconImageView.image = [UIImage imageNamed:@"cuo"];
        isVerfied.textLabel.text = @"未验证邮箱";
        
    }
    
    
    isVerfied.textLabel.font = [UIFont systemFontOfSize:14];
    isVerfied.textLabel.textColor = [UIColor grayColor];
    [self.view addSubview:isVerfied];
    
    // 通过邮箱重置密码
    self.findPW = [UIButton buttonWithType:UIButtonTypeCustom];
    self.findPW.frame = CGRectMake(KScreenWidth / 4,  KScreenHeight / 1.4, KScreenWidth / 2, KScreenWidth / 10);
    [self.findPW setTitle:@"通过邮箱重置密码" forState:UIControlStateNormal];
    self.findPW.backgroundColor = [UIColor blackColor];
    [self.findPW addTarget:self action:@selector(findPW:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_findPW];
    
    // 注销
    self.quiteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.quiteBtn.frame = CGRectMake(KScreenWidth / 4,  KScreenHeight / 1.2, KScreenWidth / 2, KScreenWidth / 10);
    [self.quiteBtn setTitle:@"注   销" forState:UIControlStateNormal];
    self.quiteBtn.backgroundColor = [UIColor redColor];
    [self.quiteBtn addTarget:self action:@selector(quiteBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_quiteBtn];
    
    
}


// 点击头像
/*
- (void)tapAction:(UIGestureRecognizer *)tap
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"提示！" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
    [actionSheet showInView:self.view];
}
#pragma mark - actionSheet代理方法 -
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 调用相机
  if (buttonIndex == 0)
    {
        NSLog(@"调用相机");
        // 判断打开系统的相机或者相册有没有成功
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            NSLog(@"打开相机成功");
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            // imagePickerController的类型，决定显示出来的效果
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;// 打开相机
            // 是否有编辑状态
//            picker.allowsEditing = YES;
            // 设置代理 点击选择的时候自动去调用代理方法
            // 需要遵守 UINavigationControllerDelegate, UIImagePickerControllerDelegate
            picker.delegate = self;
            [self presentViewController:picker animated:YES completion:nil];
        }
        else
        {
            // 警告框
            UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"没有摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        
    }
    // 调用相册
    else if (buttonIndex == 1)
    {
        NSLog(@"调用相册");
        // 判断打开系统的相机或者相册有没有成功
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            NSLog(@"打开相册成功");
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            // imagePickerController的类型，决定显示出来的效果
            //        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;// 打开相册
            // 是否有编辑状态
//            picker.allowsEditing = YES;
            // 设置代理 点击选择的时候自动去调用代理方法
            // 需要遵守 UINavigationControllerDelegate, UIImagePickerControllerDelegate
            picker.delegate = self;
            [self presentViewController:picker animated:YES completion:nil];
        }
        else
        {
            NSLog(@"打开相册失败");
        }
    }
}

#pragma mark - imagePicker代理方法 -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"ok");
    // 将选中的图片赋值给UIImageView
    // 通过字典的key值，取出对应的value值，value是我们从相册选取的图片
    self.icon.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSLog(@"%@",info);
    // 将图片存入到系统相册
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(self.icon.image, nil, nil, nil);
    }

    // 将这个界面模态回去dismiss
    [self dismissViewControllerAnimated:YES completion:nil];
}
*/


// 密码找回
- (void)findPW:(UIButton *)btn
{
    FindPWController *findPWVC = [[FindPWController alloc]init];
    [self presentViewController:findPWVC animated:YES completion:nil];
}


// 注销执行的方法
- (void)quiteBtn:(UIButton *)btn
{

    // 警告框
    UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"是否注销！" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"已注销");
        [BmobUser logout];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
   
}


@end
